package com.xingzhe.pengji.httpclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

/**
 * @author Administrator
 *
 */
public class RequestServer {

	private static Log log = LogFactory.getLog(RequestServer.class);

	public static String getJsonData(String url, String Name, String Id) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpParams httpParams = httpclient.getParams();
		// 设置网络超时参数
		HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
		HttpConnectionParams.setSoTimeout(httpParams, 5000);
		HttpPost httpPost = new HttpPost(url);
		httpPost.setParams(httpParams);
		String temp = "";
		List<NameValuePair> nvp = new ArrayList<NameValuePair>();
		nvp.add(new BasicNameValuePair(Name, Id));
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvp, "UTF-8");
			httpPost.setEntity(entity);
			HttpResponse resp = null;
			try {
				resp = httpclient.execute(httpPost);
			} catch (ConnectException e) {
				log.info("服务器不可达post" + url);
				return null;
			}
			temp = getRequestData(resp);
			log.info(temp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return temp;

	}

	public static String getJsonData(String url) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpParams httpParams = httpclient.getParams();
		// 设置网络超时参数
		HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
		HttpConnectionParams.setSoTimeout(httpParams, 5000);
		HttpGet httpGet = new HttpGet(url);
		httpGet.setParams(httpParams);
		HttpResponse resp = null;
		String temp = "";
		try {
			try {
				resp = httpclient.execute(httpGet);
			} catch (ConnectException e) {
				log.info("服务器不可达get" + url);
				return null;
			}
			temp = getRequestData(resp);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		log.info(temp);
		return temp;
	}

	private static String getRequestData(HttpResponse resp) {
		StringBuffer sb = new StringBuffer();
		HttpEntity entity2 = resp.getEntity();
		try {
			if (entity2 != null) {
				BufferedReader reader = new BufferedReader(new InputStreamReader(entity2.getContent()));
				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				reader.close();
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
		if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			//System.out.println("OK");
			return sb.toString();
		} else {
			return null;
		}
	}
}
